import { baseRequestClient, requestClient } from '#/api/request';

export namespace AuthApi {
  /** 登录接口参数 */
  export interface LoginParams {
    password?: string;
    username?: string;
  }

  /** 注册接口参数 */
  export interface RegisterParams {
    password?: string;
    username?: string;
    nickname?: string;
  }

  /** 登录接口返回值 */
  export interface LoginResult {
    accessToken: string;
  }

  export interface RefreshTokenResult {
    data: string;
    status: number;
  }

  /** 全局配置接口返回值 */
  export interface GlobalSettingResult {
    userId: number;
    createTime: string;
    updateTime: string;
    proxyIp: string;
    proxyPort: number;
    proxyActivate: boolean;
    fnosIpType: boolean;
    fnosIp: string;
    fnosPort: number;
    fnosUser: string;
    fnosPassWd: string;
    fnosSsl: boolean;
  }
}

/**
 * 登录
 */
export async function loginApi(data: AuthApi.LoginParams) {
  return requestClient.post<AuthApi.LoginResult>('/auth/login', data);
}

/**
 * 注册
 */
export async function registerApi(data: AuthApi.RegisterParams) {
  return requestClient.post<string>('/user/add', data);
}

/**
 * 刷新accessToken
 */
export async function refreshTokenApi() {
  return baseRequestClient.post<AuthApi.RefreshTokenResult>('/auth/refresh', {
    withCredentials: true,
  });
}

/**
 * 退出登录
 */
export async function logoutApi() {
  return baseRequestClient.post('/auth/logout', {
    withCredentials: true,
  });
}

/**
 * 获取用户权限码
 */
export async function getAccessCodesApi() {
  return requestClient.get<string[]>('/auth/codes');
}

/**
 * 获取用户全局配置
 */
export async function getGlobalSettingApi() {
  return requestClient.get<AuthApi.GlobalSettingResult>('/user/get/config');
}

/**
 * 设置用户全局配置
 */
export async function setGlobalSettingApi(data: AuthApi.GlobalSettingResult) {
  return requestClient.post<AuthApi.GlobalSettingResult>(
    '/user/set/config',
    data,
  );
}
